/*==================================================
Project:       Targeting Social Programs
Authors:       Diether W. Beuermann
               Bridget Hoffmann        
               Marco Stampini 
               David L. Vargas
               Diego Vera-Cossio
----------------------------------------------------
Creation Date:    May 2023
Modification Date:   
Do-file version:    01
References:          
Output:             
==================================================*/

/* Regular PMT model using all SISBEN varibles and SISBEN income
This will serve as baseline for comparison		*/


/*==================================================
            0: Program set up
==================================================*/
*Written on STATA 17
drop _all
set varabbrev off	// no variable abbreviations allowed (personal preference)

** source dir
cd "${dir5r}" // graph dir

/*==================================================
            1: load and transformations
==================================================*/

*----------  1.1. Data prep:
use "${dir3r}/01_survey/survey_targeting_r1_long.dta", clear

*----------  1.1.2 Estimate PMT for survey data:


*------------ poverty line
scalar epovline = 137350
scalar l_epovline = log(epovline)

scalar povline = 327674
scalar l_povline = log(povline)

*----------- dummys of category 


* Actual extr. poor
gen epoor = l_pp_inc_srv < l_epovline
replace epoor = . if l_pp_inc_srv == .

gen poor = l_pp_inc_srv < l_povline
replace poor = . if l_pp_inc_srv == .


g work_b=work_p if year==2019
egen aux1=max(work_b), by(id_vivienda_SISBEN)

g work_post=work_p if year==2020
egen aux2=max(work_post), by(id_vivienda_SISBEN)

g job_loss1y=aux2==0 & aux1==1

egen shock_aux= rowmax(ilness death divorce bankrupt theft conflict cut_remittance fire nat_disaster)
replace shock_aux=. if year!=2020
egen any_shock_nl=max(shock_aux), by(id_vivienda_SISBEN)

preserve 
collapse (mean) epoor poor l_pp_inc_srv [aw=pondera], by(year  job_loss1y)

/*==================================================
        2: Create figure
==================================================*/

#delimit ;

tw      (connected l_pp_inc_srv year if  job_loss1y==1,lcolor(gray)  mcolor(gray) msymbol(triangle))
        (connected l_pp_inc_srv year if  job_loss1y==0 , lcolor(gray) lpattern(dash) mcolor(gray)) 
        ,
        legend(order(1 "Job loss"
                2 "No job loss")
                pos(6) col(2))
        ytitle("Log per-capita Income")
        xtitle("Year")
        xlab(2019(1)2021)	
        name(income, replace)
;
graph export "F2_a_dynamics.png", as(png) replace ;


tw      (connected epoor year if  job_loss1y==1, lcolor(gray)  mcolor(gray) msymbol(triangle) )
        (connected epoor year if  job_loss1y==0 , lcolor(gray) lpattern(dash) mcolor(gray)) 
        ,
        legend(order(1 "Job loss"
                2 "No job loss")
                pos(6) col(2))
        ytitle("Extreme poverty rate")
        xtitle("Year")
        xlab(2019(1)2021)	
        name(epov, replace)
;
graph export "F2_b_dynamics.png", as(png) replace ;

#delimit cr 

restore 

preserve 
collapse (mean) epoor poor l_pp_inc_srv [aw=pondera], by(year  any_shock_nl)

/*==================================================
        2: Create figure
==================================================*/

#delimit ;

tw      (connected l_pp_inc_srv year if  any_shock_nl==1,lcolor(gray)  mcolor(gray) msymbol(triangle))
        (connected l_pp_inc_srv year if  any_shock_nl==0 , lcolor(gray) lpattern(dash) mcolor(gray)) 
        ,
        legend(order(1 "Shock"
                     2 "No shock")
        pos(6) col(2))
        ytitle("Log per-capita Income")
        xtitle("Year")
        xlab(2019(1)2021)	
        name(income2, replace)
;
graph export "F2_c_dynamics.png", as(png) replace ;


tw      (connected epoor year if  any_shock_nl==1, lcolor(gray)  mcolor(gray) msymbol(triangle) )
        (connected epoor year if  any_shock_nl==0 , lcolor(gray) lpattern(dash) mcolor(gray)) 
        ,
        legend(order(1 "Shock"
                2 "No shock")
                pos(6) col(2))
        ytitle("Extreme poverty rate")
        xtitle("Year")
        xlab(2019(1)2021)	
        name(epov2, replace)
;
graph export "F2_d_dynamics.png", as(png) replace ; 

#delimit cr 

restore 


graph combine income epov income2  epov2, cols(2)
graph export "F2_dynamics.png", as(png) replace
